var initSearchEngine = function($searchInput, $selectResultContainer, loaders) {
	
	var minQueryLength = 4;
	var maxResults = 10;
	var searchDelay = 500;//ms
	
	var profileLoader = loaders.profileLoader;
//	var hashTagLoader = loaders.hashTagLoader; 	TODO
	
	/**
	 * Konfiguratory zapytan
	 */
	
	var searchForProfiles = function(request, response) {
		var url = {
				type: "Search",
				method: "Profiles"
		};
		
		var success = function (data){
			var results = $.map(data, function(profile) {
							return {
								label: profile.login,
								value: "@" + profile.login,
								object: profile
							};
						});
			response(results.slice(0, maxResults));
		};
		
		search(request, response, url, success);
	};
	
	var searchForEntries = function(request, response) {
		var url = {
				type: "Search",
				method: "Entries",
				param: "output,clear"
		};
		
		var success = function (data){
			var results = $.map(data, function(entry) {
							return {
								label: entry.body.length > 50 ? entry.body.substring(0, 50) : entry.body,
								value: entry.body,
								object: entry
							};
						});
			response(results.slice(0, maxResults));
		};
		
		search(request, response, url, success);
	};
	
	var searchForHashTags = function(request, response) {
		var results = $.map(HashTags.getLike(request.term), function(tag) {
			return {
				label: "#" + tag.name,
				value: "#" + tag.name,
				object: tag
			};
		});
		response(results.slice(0, maxResults));
	};
	
	/**
	 * Wysyla zapytanie do serwera
	 */
	
	var search = function(request, response, url, success) {
		var query = request.term;
		
		console.log("Wyszukuje: "+query);
		
		var params = {
			url: url,
			post: {q: query},
			success: success,
			error: function(e) {
				response(["BLAD: Cos poszlo zle..."]);
				console.log("error");
				console.log(e);
			}
		};
		
		Request.send(params);
	};
	
	/**
	 * Akcje po zaznaczeniu jednej z pozycji autopodpowiedzi
	 */
	
	var selectProfile = function(profile) {
		profileLoader(profile, true, $selectResultContainer);
	};
	
	var selectHashTag = function(hashtag) {
//TODO		hashTagLoader(hashtag, true, $$selectResultContainer);
	};
	
	var selectEntry = function(entry) {
		MainContent.clear();
		MainContent.append(new Entry(entry, true, true).getComponent());
		MainContent.show();
	};
	
	/**
	 * Renderuje elementy na liscie widocznej jako autouzupelnianie
	 */
	var renderer = function(ul, item) {
		if(item.value.indexOf("@") === 0) {
			var $template = SearchTemplate.profile();
			$template.find(".search_avatar").attr("src", item.object.avatar_lo);
			$template.find(".search_text").text( item.label );
			
			return $template.appendTo( ul );
			
		}else if(item.value.indexOf("#") === 0) {
			var $template = SearchTemplate.profile();
			$template.find(".search_text").text( item.label );
			
			return $template.appendTo( ul );
			
		}else {
			var $template = SearchTemplate.profile();
			$template.find(".search_text").text( item.label );
			
			return $template.appendTo( ul );
		}
	};
	
	
	// ##### Component initialization #####
	
	$searchInput.autocomplete({
		source: 
			function(request, response) {
				if(request.term.indexOf("@") === 0) {
					request.term = request.term.substring(1, request.term.length);
					searchForProfiles(request, response);
					
				}else if(request.term.indexOf("#") === 0) {
					request.term = request.term.substring(1, request.term.length);
					searchForHashTags(request, response);
					
				}else {
					searchForEntries(request, response);
				}
			},
		minLength: minQueryLength < 4 ? 4 : minQueryLength,
		delay: searchDelay,
		appendTo: null,
		select: 
			function(event, ui) {
				if(ui.item) {
					if(ui.item.value.indexOf("@") === 0) {
						selectProfile(ui.item.object);
						
					}else if(ui.item.value.indexOf("#") === 0) {
						selectHashTag(ui.item.object);
						
					}else {
						selectEntry(ui.item.object);
					}
				}
				
				//reset input
				$(this).val("");
				return false;
			}
	}).data('ui-autocomplete')._renderItem = renderer;
	
	$searchInput.keyup(function() {
		if($(this).val().indexOf("#") == 0) {
			$searchInput.autocomplete( "option", "minLength", 2 );
			$searchInput.autocomplete( "option", "delay", 100 );
		} else {
			$searchInput.autocomplete( "option", "minLength", minQueryLength );
			$searchInput.autocomplete( "option", "delay", searchDelay );
		}
	});
};